package com.ruoyi.stock.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.stock.domain.StockSector;
import com.ruoyi.stock.domain.query.StockSectorQuery;
import com.ruoyi.stock.mapper.StockSectorMapper;
import com.ruoyi.stock.service.impl.test.LonghuDataProcessor;
import com.ruoyi.stock.service.impl.test.LonghuResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.stock.mapper.StockInfoMapper;
import com.ruoyi.stock.domain.StockInfo;
import com.ruoyi.stock.service.IStockInfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 股票池信息Service业务层处理
 * 
 * @author lzl
 * @date 2025-11-05
 */
@Service
public class StockInfoServiceImpl  extends ServiceImpl<StockInfoMapper,StockInfo> implements IStockInfoService
{
    @Autowired
    private StockInfoMapper stockInfoMapper;
    @Autowired
    private StockSectorMapper stockSectorMapper;

    /**
     * 查询股票池信息
     * 
     * @param id 股票池信息主键
     * @return 股票池信息
     */
    @Override
    public StockInfo selectStockInfoById(Long id)
    {
        return stockInfoMapper.selectStockInfoById(id);
    }

    /**
     * 查询股票池信息列表
     * 
     * @param stockInfo 股票池信息
     * @return 股票池信息
     */
    @Override
    public List<StockInfo> selectStockInfoList(StockInfo stockInfo)
    {
        return stockInfoMapper.selectStockInfoList(stockInfo);
    }

    /**
     * 新增股票池信息
     * 
     * @param stockInfo 股票池信息
     * @return 结果
     */
    @Override
    public int insertStockInfo(StockInfo stockInfo)
    {
        return stockInfoMapper.insert(stockInfo);
    }

    /**
     * 修改股票池信息
     * 
     * @param stockInfo 股票池信息
     * @return 结果
     */
    @Override
    public int updateStockInfo(StockInfo stockInfo)
    {
        return stockInfoMapper.updateById(stockInfo);
    }

    /**
     * 批量删除股票池信息
     * 
     * @param ids 需要删除的股票池信息主键
     * @return 结果
     */
    @Override
    public int deleteStockInfoByIds(String ids)
    {
        return stockInfoMapper.deleteStockInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除股票池信息信息
     * 
     * @param id 股票池信息主键
     * @return 结果
     */
    @Override
    public int deleteStockInfoById(Long id)
    {
        return stockInfoMapper.deleteById(id);
    }

    /**
     * 插入股票池信息
     *
     * @param setorId       股票板块信息ID
     * @param code          股票代码
     * @param isHistoryDate 是否历史日期
     * @param historyDate   历史日期
     * @return
     */
    public List<StockInfo> handlerStockInfo(Long setorId, String code, Boolean isHistoryDate, String historyDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        LonghuDataProcessor processor = new LonghuDataProcessor();
        String url = "https://apphq.longhuvip.com/w1/api/index.php?Order=1&st=30&a=ZhiShuStockList_W8&c=ZhiShuRanking&PhoneOSNew=1&old=1&apiv=w21&Type=6&PlateID="+code+"&";
        if(isHistoryDate) {
            url = "https://apphis.longhuvip.com/w1/api/index.php?Order=1&st=30&a=ZhiShuStockList_W8&c=ZhiShuRanking&PhoneOSNew=1&old=1&apiv=w21&Type=6&PlateID="+code+"&Date="+historyDate;
        }
        LonghuResponse response1 = processor.fetchData(url);
        List<List<Object>> list2 = response1.getList();
        List<StockInfo> stockInfos = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list2)) {
            for (List<Object> ls : list2) {
                if(CollectionUtils.isNotEmpty(ls)) {
                    StockInfo stockInfo = new StockInfo();
                    stockInfo.setCode((String) ls.get(0));
                    stockInfo.setName((String) ls.get(1));
                    stockInfo.setIncreaseRate((Double) ls.get(7));
                    stockInfo.setRiseSpeed("");
                    stockInfo.setLeadUp((String) ls.get(24));
                    stockInfo.setMainForce((String) ls.get(2));
                    stockInfo.setConsecutiveBoards((String) ls.get(23));
                    stockInfo.setTheme("");
                    stockInfo.setMainNetAmount((Double) ls.get(14));
                    try {
                        stockInfo.setRecordDate(sdf.parse(historyDate));
                    } catch (Exception e){
                        e.printStackTrace();
                    }
                    stockInfo.setSetorId(setorId);
                    stockInfos.add(stockInfo);
                }
            }
            this.saveBatch(stockInfos);
        }
        System.out.println("    "+ JSON.toJSONString(stockInfos));
        return stockInfos;
    }

    /**
     * 获取股票池列表
     * @param stockSectorQuery
     * @return
     */
    @Override
    public List<StockInfo> getStockInfo(StockSectorQuery stockSectorQuery) {
        String recordDate = stockSectorQuery.getRecordDate();
        //判断是否有日期的数据
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("record_date", recordDate);
        queryWrapper.eq("setor_id",stockSectorQuery.getSetorId());
        long count = this.count(queryWrapper);
        if(count > 0) {
            return this.list(queryWrapper);
        } else {
            //判断是否是历史日期
            Boolean isHistoryDate = false;
            String historyDate = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if(!sdf.format(new Date()).equals(recordDate)) {
                isHistoryDate = true;
                historyDate = recordDate;
                StockSector stockSector = stockSectorMapper.selectById(stockSectorQuery.getSetorId());
                return handlerStockInfo(stockSector.getId(),stockSector.getCode(),isHistoryDate,historyDate);

            } else {
                return Lists.newArrayList();
            }
        }
    }
}
